Skip to content

[ZH] Fix loop index comparison warnings #804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Mauller
Copy link

@Mauller Mauller commented May 3, 2025

This PR fixes loop index comparison warnings by matching the data type of the loop index to match the comparison values data type.

TODO:

  • Replicate in Generals.

@Mauller Mauller force-pushed the fix-loop-index-warnings branch from 14f7171 to 8aa4016 Compare May 3, 2025 16:32
@Mauller Mauller marked this pull request as ready for review May 3, 2025 16:39
@Mauller Mauller self-assigned this May 3, 2025
@Mauller Mauller added Minor Severity: Minor < Major < Critical < Blocker Build Anything related to building, compiling ZeroHour Relates to Zero Hour labels May 3, 2025
@aliendroid1
Copy link

If you are going to change int to an unsigned type then I think std::size_t would be more appropriate and in line with standard conventions. Where range loops can be used then they should be preferred

Copy link

@xezon xezon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fairly safe, but I suggest to give it a test for mismatch, especially with a focus on USA Countermeasures.

size_t is identical to std::size_t.

@@ -413,7 +413,7 @@ static void findHighFileNumber( AsciiString filename, void *userData )

// strip off the extension at the end of the filename
AsciiString nameOnly = filename;
for( Int count = 0; count < strlen( SAVE_GAME_EXTENSION ); count++ )
for( size_t count = 0; count < strlen( SAVE_GAME_EXTENSION ); count++ )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this will call strlen on every iteration...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i think the VC6 version may do that but modern MSVC would likely compile it out as a constant.

@@ -579,7 +579,7 @@ Bool MapCache::loadUserMaps()
{
AsciiString endingStr;
AsciiString fname = s+1;
for (Int i=0; i<strlen(mapExtension); ++i)
for (size_t i=0; i<strlen(mapExtension); ++i)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another strlen in condition...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah... i didn't want to change too much.

@@ -301,7 +301,7 @@ void CountermeasuresBehavior::launchVolley()
Object *obj = getObject();

Real volleySize = (Real)data->m_volleySize;
for( int i = 0; i < data->m_volleySize; i++ )
for( UnsignedInt i = 0; i < data->m_volleySize; i++ )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope this will produce the same float value below.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory it should as the raw binary values of positive signed int and unsigned int values are the same up to 2.7 billion at least.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes 34% of assembler in CountermeasuresBehaviour::launchVolley.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting how it changes that much when the index is only used in one place.

@Mauller Mauller force-pushed the fix-loop-index-warnings branch from 8aa4016 to f4c5b85 Compare May 4, 2025 07:26
@Mauller
Copy link
Author

Mauller commented May 4, 2025

Updated this with the inclusion of a cast that was missing in one of the loops.

@Mauller Mauller force-pushed the fix-loop-index-warnings branch from f4c5b85 to c0eff59 Compare May 4, 2025 16:40
@Mauller
Copy link
Author

Mauller commented May 4, 2025

Just rebased off recent main after the other warning fixes were merged.

@Mauller
Copy link
Author

Mauller commented May 4, 2025

Just ran Golden Replay 1 against this with the VC6 build and it did not mismatch.
Not sure if countermeasures were explicitly used by any of the USA players. But they were using every vehicles in the game during the test.

So I think the changes are fairly safe, i can replicate the changes to generals if people are happy.

Copy link

@xezon xezon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unassemblize shows significant assembler changes in the following 4 functions. I suggest to focus mismatch test on functionality related to these.

ScriptActions::doMoveTeamTowardsNearest
DeliverPayloadNugget::create
W3DLaserDraw::W3DLaserDraw
CountermeasuresBehaviour::launchVolley

@@ -5296,7 +5296,7 @@ void ScriptActions::doMoveTeamTowardsNearest( const AsciiString& teamName, const
{
Real closestDist;
Real dist;
for( Int typeIndex = 0; typeIndex < objectTypes->getListSize(); typeIndex++ )
for( size_t typeIndex = 0; typeIndex < objectTypes->getListSize(); typeIndex++ )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates a strange diff in ScriptActions::doMoveTeamTowardsNearest

image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i don't get that, if anything i would have through that most code changes would be reduction in code due to not needing to perform some casts.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks as if the function call has changed, but perhaps it is just an error in the tool.

@@ -322,7 +322,7 @@ class DeliverPayloadNugget : public ObjectCreationNugget
}

Object *firstTransport = NULL;
for( Int formationIndex = 0; formationIndex < m_formationSize; formationIndex++ )
for( UnsignedInt formationIndex = 0; formationIndex < m_formationSize; formationIndex++ )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This innocent looking edit changes 13% of the assembler inside DeliverPayloadNugget::create.

image

@@ -156,7 +156,7 @@ W3DLaserDraw::W3DLaserDraw( Thing *thing, const ModuleData* moduleData ) :
//Allocate an array of lines equal to the number of beams * segments
m_line3D = NEW SegmentedLineClass *[ data->m_numBeams * data->m_segments ];

for( int segment = 0; segment < data->m_segments; segment++ )
for( UnsignedInt segment = 0; segment < data->m_segments; segment++ )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes 55% of the assembler in W3DLaserDraw::W3DLaserDraw.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats interesting, you would think it would just be a few casts that got changed since segment is only used it one place within the function

@@ -301,7 +301,7 @@ void CountermeasuresBehavior::launchVolley()
Object *obj = getObject();

Real volleySize = (Real)data->m_volleySize;
for( int i = 0; i < data->m_volleySize; i++ )
for( UnsignedInt i = 0; i < data->m_volleySize; i++ )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes 34% of assembler in CountermeasuresBehaviour::launchVolley.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Build Anything related to building, compiling Minor Severity: Minor < Major < Critical < Blocker ZeroHour Relates to Zero Hour
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants